{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# {func}`mlc.cc.jit_load`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import mlc\n",
    "import mlc.dataclasses as mlcd\n",
    "import pytest\n",
    "from mlc._cython import SYSTEM"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Executing command: /usr/bin/ccache /usr/bin/x86_64-linux-gnu-g++-11 -O2 -g -shared -fPIC -std=c++17 -DMLC_COMPILATION=1 -fvisibility=hidden -o main.so -I/media/pc/data/lxw/ai/mlc-python/include -I/media/pc/data/lxw/ai/mlc-python/3rdparty/dlpack/include _mlc_source_0.cc\n"
     ]
    }
   ],
   "source": [
    "mlc.cc.jit_load(\"\"\"\n",
    "#include <mlc/core/all.h>\n",
    "#include <string>\n",
    "\n",
    "struct MyObj : public mlc::Object {\n",
    "mlc::Str x;\n",
    "int32_t y;\n",
    "MyObj(mlc::Str x, int y) : x(x), y(y) {}\n",
    "int32_t YPlusOne() const { return y + 1; }\n",
    "MLC_DEF_DYN_TYPE(MyObj, Object, \"mlc.MyObj\");\n",
    "};\n",
    "\n",
    "struct MyObjRef : public mlc::ObjectRef {\n",
    "MLC_DEF_OBJ_REF(MyObjRef, MyObj, mlc::ObjectRef)\n",
    "    .Field(\"x\", &MyObj::x)\n",
    "    .FieldReadOnly(\"y\", &MyObj::y)\n",
    "    .StaticFn(\"__init__\", mlc::InitOf<MyObj, mlc::Str, int32_t>)\n",
    "    .MemFn(\"YPlusOne\", &MyObj::YPlusOne);\n",
    "};\n",
    "\"\"\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "@mlcd.c_class(\"mlc.MyObj\")\n",
    "class MyObj(mlc.Object):\n",
    "    x: str\n",
    "    y: int\n",
    "\n",
    "    def YPlusOne(self) -> int:\n",
    "        return type(self)._C(b\"YPlusOne\", self)\n",
    "\n",
    "obj = MyObj(\"hello\", 42)\n",
    "assert obj.x == \"hello\"\n",
    "assert obj.y == 42\n",
    "assert obj.YPlusOne() == 43\n",
    "\n",
    "obj.x = \"world\"\n",
    "assert obj.x == \"world\"\n",
    "with pytest.raises(TypeError):\n",
    "    obj.x = 42  # type: ignore[assignment]\n",
    "with pytest.raises(AttributeError):\n",
    "    obj.y = 42\n",
    "del obj"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "ai",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
